# 路由和视图函数
import random
import time

from flask import Blueprint, render_template, request, jsonify
from sqlalchemy import desc, and_, or_, not_

from .exts import cache

from .models import *

# blue = Blueprint('user', __name__)

# @blue.route('/addgrade')
# def add_grade():
#     grades = []
#     for i in range(10):
#         grade = Grade()
#         grade.name = 'lzr' + str(i)
#         grades.append(grade)
#     try:
#         db.session.add_all(grades)
#         db.session.commit()
#     except Exception as e:
#         print('e:', e)
#         db.session.rollback()
#         db.session.flush()
#     return 'index'


# @blue.route('/addstu')
# def add_stu():
#     stus = []
#     for i in range(20,30):
#         stu = Student()
#         stu.name = 'qxl' + str(i)
#         stu.age = i
#         stu.gradeid = random.randint(9,10)
#         stus.append(stu)
#     try:
#         db.session.add_all(stus)
#         db.session.commit()
#     except Exception as e:
#         db.session.rollback()
#         db.session.flush()
#     return 'success'
#
#
# # 修改
# @blue.route('/updatestu')
# def update_stu():
#     stu = Student.query.first() # 获取第一条数据
#     stu.age = 100
#     db.session.commit()
#     return 'success'
#
#
# # 删除
# @blue.route('/delstu')
# def del_stu():
#     stu = Student.query.first()
#     db.session.delete(stu)
#     db.session.commit()
#     return 'success'
#
# # 查询
# @blue.route('/getstu')
# def get_stu():
#     # stu = Student.query.get(2)
#     # print(stu.name, stu.age)
#     # print(stu.gradeid, stu.grade.name, stu.grade.id)
#     grade = Grade.query.get(10)
#     print(grade.name, grade.students)
#     for i in grade.students:
#         print(i.name, i.gradeid)
#     return 'success'
#
#
#



# # 添加用户数据
# @blue.route('/adduser')
# def add_user():
#     user_list = []
#     for i in range(10, 20):
#         user = UserModel()
#         user.name = 'lzr' + str(i)
#         user.age = i
#         user_list.append(user)
#     try:
#         db.session.add_all(user_list)
#         db.session.commit()
#     except Exception as e:
#         print(e)
#         db.session.rollback()
#         db.session.flush()
#     return 'Ok'
#
#
# # 添加电影
# @blue.route('/addmovie')
# def add_movie():
#     movie_list = []
#     for i in range(10, 20):
#         movie = Movie()
#         movie.name = '星球大战' + str(i)
#         movie_list.append(movie)
#     try:
#         db.session.add_all(movie_list)
#         db.session.commit()
#     except Exception as e:
#         db.session.rollback()
#         db.session.flush()
#     return 'OK'
#
#
# @blue.route('/addcollect')
# def add_collect():
#     user = UserModel.query.get(1)
#     movie = Movie.query.get(7)
#     user.movies.append(movie)
#     db.session.commit()
#     return 'OK'
#
#
# @blue.route('/getcollect')
# def get_collect():
#     # 查找用户1所收藏的所有电影
#     user  = UserModel.query.get(1)
#     print(user.movies)
#     # 查询某个电影被哪些用户收藏
#     movie = Movie.query.get(1)
#     print(list(movie.users))
#     return 'ok'
#
# # 删除用户, 看表关系
# @blue.route('/deluser')
# def del_user():
#     user = UserModel.query.get(1)
#     db.session.delete(user)
#     db.session.commit()
#     return 'ok'

# @blue.route('/home')
# @cache.cached(timeout=20)
# def home():
#     print('Index2')
#     time.sleep(5)
#     return 'Index2'
#
# # 钩子函数 中间件
# # before_request 每次请求之前都会访问
# @blue.before_request
# def before_request():
#     print('before_request')
#     # print(request.user_agent)
#     # if "python" in request.user_agent.string:
#     #     return "您正在使用python爬虫, 再见!"
#     # ip = request.remote_addr
#     # if cache.get(ip):
#     #     return '小伙子别爬了'
#     # else:
#     #     cache.set(ip, 'value', timeout=1)
#     # return None


# @blue.route('/', methods=['GET', 'POST'])
# def index():
#     if request.method == 'GET':
#         return jsonify({"methods":"GET"})
#     elif request.method == 'POST':
#         return jsonify({"methods":"POST"})



